/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.vista.connexio;

import comandaXM.utils.ButtonTabComponent;
import comandaXM.utils.JOPtionPaneXM;
import comandaXM.utils.TDSLanguageUtils;
import comandaXM.vista.auxiliars.JDIves;
import comandaXM.vista.auxiliars.JDPaisos;
import comandaXM.vista.auxiliars.JDProvincies;
import comandaXM.vista.auxiliars.JDTipusPagament;
import comandaXM.vista.manteniment.centres.JPCentre;
import comandaXM.vista.manteniment.proveidor.JPProveidor;



/**
 *
 * @author xmarti
 */
public class JFComandaXM extends javax.swing.JFrame {

    private JOPtionPaneXM optionPane= new JOPtionPaneXM(); 
    public JFComandaXM() {
        initComponents();
  
        this.setTitle(TDSLanguageUtils.getMessage("JFComanda.frame.caption"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jTabbedPane = new javax.swing.JTabbedPane();
        jToolBar = new javax.swing.JToolBar();
        jMenu = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuManteniment = new javax.swing.JMenu();
        jItemProveidor = new javax.swing.JMenuItem();
        jItemCentres = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuPaisos = new javax.swing.JMenuItem();
        jMenuProvincies = new javax.swing.JMenuItem();
        jMenuTipusIva = new javax.swing.JMenuItem();
        jMenuTipusPagament = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        jMenuItem3.setText("jMenuItem3");

        jMenu2.setText("jMenu2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setExtendedState(6);

        jToolBar.setRollover(true);

        jMenu1.setText("File");
        jMenu.add(jMenu1);

        jMenuManteniment.setText("Manteniment");

        jItemProveidor.setText("Proveidors");
        jItemProveidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jItemProveidorActionPerformed(evt);
            }
        });
        jMenuManteniment.add(jItemProveidor);

        jItemCentres.setText("CENTRES");
        jItemCentres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jItemCentresActionPerformed(evt);
            }
        });
        jMenuManteniment.add(jItemCentres);

        jMenu3.setText("Auxiliars");

        jMenuPaisos.setText("Paissos");
        jMenuPaisos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuPaisosActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuPaisos);

        jMenuProvincies.setText("Provincies");
        jMenuProvincies.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuProvinciesActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuProvincies);

        jMenuTipusIva.setText("Tipus d'iva");
        jMenuTipusIva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuTipusIvaActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuTipusIva);

        jMenuTipusPagament.setText("Tipus Pagament");
        jMenuTipusPagament.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuTipusPagamentActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuTipusPagament);

        jMenuManteniment.add(jMenu3);

        jMenu.add(jMenuManteniment);

        setJMenuBar(jMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 881, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane)
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jItemProveidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jItemProveidorActionPerformed
        // Ens cal afegir la pestanya proveidors
        //primer cal Trobar la finestra proveïdors i si no la troba, crear-la 
        setAction ("proveidor"); 
    }//GEN-LAST:event_jItemProveidorActionPerformed

    private void jItemCentresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jItemCentresActionPerformed
        setAction ("centre"); 
    }//GEN-LAST:event_jItemCentresActionPerformed

    private void jMenuPaisosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuPaisosActionPerformed
        final JDPaisos dialog = new JDPaisos() {
            @Override
            public void actualitzar() {
                actualitzarPaisos(); 
            }
        };               
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
       
    }//GEN-LAST:event_jMenuPaisosActionPerformed

    private void jMenuProvinciesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuProvinciesActionPerformed
        final JDProvincies dialog = new JDProvincies() {
            @Override
            public void actualitzar() {
                actualitzarProvincies(); 
            }
        };               
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
       
    }//GEN-LAST:event_jMenuProvinciesActionPerformed

    private void jMenuTipusIvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuTipusIvaActionPerformed
        final JDIves  dialog = new JDIves() {
            @Override
            public void actualitzar() {
                actualitzarTipusIva(); 
            }

            
        };               
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);

    }//GEN-LAST:event_jMenuTipusIvaActionPerformed

    private void jMenuTipusPagamentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuTipusPagamentActionPerformed
        final JDTipusPagament  dialog = new JDTipusPagament() {
            @Override
            public void actualitzar() {
                actualitzarTipusPagament(); 
            }
        };               
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);

    }//GEN-LAST:event_jMenuTipusPagamentActionPerformed

    @SuppressWarnings("SuspiciousIndentAfterControlStatement")
    private void setAction (String action){
        int i =-1;
        
        switch (action){
                case "proveidor":                                        
                    i = getTabIndex(TDSLanguageUtils.getMessage("JFComandaXM.jtabPane.proveidor.caption"));
                    if (i>=0 ){
                        jTabbedPane.setSelectedIndex(i);
                    }else{                        
                        //final JPAnimal p1= new JPAnimal(implMaintenance,this.actions,Cow.class.getName());
                        JPProveidor p1= new JPProveidor(); 
                        jTabbedPane.add(p1,TDSLanguageUtils.getMessage("JFComandaXM.jtabPane.proveidor.caption"));              
                        if(jTabbedPane.getTabCount()>0)
                        jTabbedPane.setTabComponentAt(jTabbedPane.getTabCount()-1,new ButtonTabComponent (jTabbedPane) {
                            
                        });                        
                        jTabbedPane.setSelectedIndex(jTabbedPane.getTabCount()-1);                    
                    }                                       
                    break; 
                   case "centre":
                       i = getTabIndex(TDSLanguageUtils.getMessage("JFComandaXM.jtabPane.centre.caption"));
                    if (i>=0 ){
                        jTabbedPane.setSelectedIndex(i);
                    }else{                        
                        //final JPAnimal p1= new JPAnimal(implMaintenance,this.actions,Cow.class.getName());
                        JPCentre p1= new JPCentre();
                        jTabbedPane.add(p1,TDSLanguageUtils.getMessage("JFComandaXM.jtabPane.centre.caption"));              
                        if(jTabbedPane.getTabCount()>0)
                        jTabbedPane.setTabComponentAt(jTabbedPane.getTabCount()-1,new ButtonTabComponent (jTabbedPane) {
                            
                        });                        
                        jTabbedPane.setSelectedIndex(jTabbedPane.getTabCount()-1);                    
                    }                                       
                    break; 
                       
                    
            };
    }
     
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Metal".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFComandaXM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFComandaXM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFComandaXM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFComandaXM.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TDSLanguageUtils.setDefaultLanguage("comandaXM/i18n/messages");
                new JFComandaXM().setVisible(true);
            }
        });
    }
   private int getTabIndex(String title){                
        for(int i =0; i<jTabbedPane.getTabCount();i++){
            if (jTabbedPane.getTitleAt(i).equals(title)) return i; 
        }        
        return -1; 
    }
   private void actualitzarTipusIva(){
       for(int i=0; i<this.jTabbedPane.getTabCount();i++){
            switch(this.jTabbedPane.getComponent(i).toString()){
                case "JPProveidor":
                    ((JPProveidor) this.jTabbedPane.getComponent(i)).setTipusIva();                                           
                    break;                                          
            }                                     
        }
   }
   private void actualitzarTipusPagament(){
       for(int i=0; i<this.jTabbedPane.getTabCount();i++){
            switch(this.jTabbedPane.getComponent(i).toString()){
                case "JPProveidor":
                    ((JPProveidor) this.jTabbedPane.getComponent(i)).setTipusPagament();
                    break;                                          
            }                                     
        }
   }
   private void actualitzarProvincies(){
       for(int i=0; i<this.jTabbedPane.getTabCount();i++){
            switch(this.jTabbedPane.getComponent(i).toString()){
                case "JPProveidor":
                    ((JPProveidor) this.jTabbedPane.getComponent(i)).setProvincies();
                    break;                                          
            }                                     
        }
   }
   private void actualitzarPaisos(){
       for(int i=0; i<this.jTabbedPane.getTabCount();i++){
            switch(this.jTabbedPane.getComponent(i).toString()){
                case "JPProveidor":
                    ((JPProveidor) this.jTabbedPane.getComponent(i)).setPaisos();
                    break;                                          
            }                                     
        }
   }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jItemCentres;
    private javax.swing.JMenuItem jItemProveidor;
    private javax.swing.JMenuBar jMenu;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenu jMenuManteniment;
    private javax.swing.JMenuItem jMenuPaisos;
    private javax.swing.JMenuItem jMenuProvincies;
    private javax.swing.JMenuItem jMenuTipusIva;
    private javax.swing.JMenuItem jMenuTipusPagament;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JToolBar jToolBar;
    // End of variables declaration//GEN-END:variables
}
